package com.jn.glide.views;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson2.JSON;
import com.jn.glide.R;
import com.jn.glide.adapter.TitlesAdapter1;
import com.jn.glide.base.BaseActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;

public class RetrofitGlideActivity extends BaseActivity {
    private RecyclerView mRecyclerView;
    private static List<RetrofitGlideActivity.Entity1.Data> mList;
    private static TitlesAdapter1 mAdapter;
    @SuppressLint("HandlerLeak")
    public static Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 10) {
                mAdapter.updateData(mList);
                mAdapter.notifyDataSetChanged();
            }
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_avatar;
    }

    @Override
    public void init() {
        mRecyclerView = findViewById(R.id.recycler);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mList = new ArrayList<>();
        mAdapter = new TitlesAdapter1(mList);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.addItemClickListener(new TitlesAdapter1.OnItemClickListener() {
            @Override
            public void onItemClick(RetrofitGlideActivity.Entity1.Data item) {
                Intent intent = new Intent(RetrofitGlideActivity.this, RetrofitGlide2Activity.class);
                intent.putExtra("bean", item);
                startActivity(intent);
            }
        });
        request();
    }

    public void request() {
//创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.oioweb.cn/api/picture/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
//创建网络请求接口的实例
        GetRequest_Interface request = retrofit.create(GetRequest_Interface.class);
//对发送送请求进行封装
        Call<Entity1> call = request.getDate();
//发送网络请求(异步)
        call.enqueue(new Callback<Entity1>() {
            @Override
            public void onResponse(Call<Entity1> call, Response<Entity1> response) {
                RetrofitGlideActivity.Entity1 entity = response.body();
                mList = entity.result;
                //这里是子线程不能更新UI
                handler.sendEmptyMessage(10);
            }

            @Override
            public void onFailure(Call<Entity1> call, Throwable t) {
                Log.i("onFailure", "请求失败");
            }
        });
    }

    public class Entity1 {
        public int code;
        public String msg;
        public ArrayList<Data> result;

        public class Data implements Serializable {
            public String id;
            public String name;
            public String num;
            public String sort_order;
            public String icon;
            public String status;
            public String type;
            public ArrayList<Data2> list;
        }

        public class Data2 implements Serializable {
            public String id;
            public String name;
            public String sort_order;
            public String icon;
            public String status;
        }
    }

    public interface GetRequest_Interface {

        @GET("miyoushe_avatar")
        Call<Entity1> getDate();
    }
}
